<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Admin extends Controller
{
    public function index()
    {
        return view("index");
    }

    //用户添加
    public function add()
    {

        if (input('sub'))
        {
            date_default_timezone_set('PRC');
            $time = date("Y-m-d H:i:s");
            $uname = input('name');
            $email=input('email');
            $pwd = input('pwd');
            $cha = db('admin')->where(['m_name' => $uname])->find();
            if ($cha)
            {
                echo $this->error('添加失败,用户名已被占用', 'index/admin/add');
            }
            else
            {
                $rs = db('admin')->insert([
                    'm_name' => $uname,
                    'm_pwd' => $pwd,
                    'm_time' => $time,
                    'm_email'=>$email
                ]);
                //echo db('stu')->getlastsql();
                if ($rs)
                {
                    echo '<div style="font-size: 26px;margin-left:200px;margin-top:150px;"><span style="color:blue;font-size:40px;">添加成功</span></div><script>setTimeout(\'window.parent.location.reload();\',1000)</script>';
                }
                else
                {
                    echo '<div style="font-size: 26px;margin-left:200px;margin-top:150px;"><span style="color:blue;font-size:40px;">添加失败</span></div><script>setTimeout(\'window.parent.location.reload();\',1000)</script>';
                }
            }
        }
        else
        {
            return view('add');
        }
    }

    //用户修改
    public function update()
    {

        if (input('sub'))
        {
            //$uname = input('name');
            $pwd1 = input('oldpwd');
            $pwd = input('newpass');
            $email=input('email');
            $hid = input('hid');
            $ch = db('admin')->where('m_id', $hid)->find();
            if ($pwd1 == $ch['m_pwd'])
            {
                $rs = db('admin')
                    ->where('m_id', $hid)
                    ->update([
                        'm_email'=>$email,
                        'm_pwd' => $pwd
                    ]);
                if ($rs)
                {
                    //echo $this->success('修改成功', 'index/admin/index');
                    echo '<div style="font-size: 26px;margin-left:250px;margin-top:150px;"><span style="color:blue;font-size:50px;">修改成功</span></div><script>setTimeout(\'window.parent.location.reload();\',1000)</script>';
                    //window.location.
                }
                else
                {
                    //echo $this->error('修改失败', 'index/admin/index');
                    echo '<div style="font-size: 26px;margin-left:250px;margin-top:150px;"><span style="color:blue;font-size:50px;">修改失败</span></div><script>setTimeout(\'window.parent.location.reload();\',1000)</script>';
                }
                //echo db('stu')->getlastsql();
            } else {
                //echo $this->error('原密码不正确', 'index/admin/index');
                echo '<div style="font-size: 26px;margin-left:250px;margin-top:150px;"><span style="color:blue;font-size:50px;">原密码不正确</span></div><script>setTimeout(\'window.parent.location.reload();\',1000)</script>';
            }
        }
        else
        {
            $id = input('m_id');
            $rs = db('admin')
                ->where('m_id', $id)
                ->find();
            return view('update', ['rs' => $rs]);
        }
    }

    /*//修改密码
    public function uupdate(){

             if(null!==input('submit'))
             {

                 $pwd=sha1(input('pwd'));
                 $pwd1=sha1(input('pwd1'));
                 $pwd2=sha1(input('pwd2'));
                 $hid=input('hid');
                 $ch=db('jf_user') ->where('uname',$hid) ->find();
                 if($pwd1==$pwd2){
                     if($ch['upwd']==$pwd){

                 $rs=db('jf_user')
                        ->where('uname',$hid)
                        ->update([
                        'upwd'=>$pwd1
                    ]);
                    //echo db('stu')->getlastsql();
                    if($rs)
                        echo $this->success('修改成功','index/index/Tuichu');
                    else
                        echo $this->error('修改失败,原密码与新密码一致','index/User/uupdate');
                 }else{ echo $this->success('原密码错','index/User/uupdate');}
                 }else{   echo $this->success('密码不一致','index/User/uupdate');   }
             }else{

                 return view('uupdate');
             }
    }
    //用户删除*/
    public function del()
    {
        $id = input('m_id');
        $rs = db('admin')->where('m_id', $id)->delete();
        /*if ($rs)
            $this->success('删除成功', 'index/admin/index');
        else
            $this->error('删除失败', 'index/admin/index');*/
    }

    //用户注册
    public function zhu()
    {
        if (input('sub'))
        {
            date_default_timezone_set('PRC');
            $time = date("Y-m-d H:i:s");
            $uname = input('m_name');
            $email=input('email');
            $pwd = input('pwd');
            $cha = db('admin')->where(['m_name' => $uname])->find();
            if ($cha)
            {
                echo $this->error('添加失败,用户名已被占用', 'index/admin/zhu');
            }
            else
            {
                $rs = db('admin')->insert([
                    'm_name' => $uname,
                    'm_pwd' => $pwd,
                    'm_time' => $time,
                    'm_email'=>$email
                ]);
                //echo db('stu')->getlastsql();
                if ($rs)
                {
                    echo $this->success('注册成功，请登录...', 'index/index/index');
                }
                else
                {
                    echo $this->error('添加失败', 'index/admin/zhu');
                }
            }
        }
        else
        {
            return view('zhu');
        }

    }


    //找回密码
    public function wang()
    {
        if(input('sub'))
        {
            $uname = input('m_name');
            $email=input('email');
            $pwd=input('pwd');
            $cha = db('admin')->where(['m_name' => $uname,'m_email'=>$email])->find();
            Session::set('email',$cha['m_email']);
            //echo session('name');die();
            if(input('pwd')!='')
            {
                $name=session('name');
                //echo $pwd;die();
                $xiu=db('admin')->where('m_name',$name)->update(['m_pwd'=>$pwd]);
                //print_r($xiu);die();
                if ($xiu)
                {
                    echo $this->success('密码重置成功！', 'index/index/login');
                }
                else
                {
                    echo $this->error('密码重置失败','index/admin/wang');
                }
            }
            if ($cha)
            {
                Session::set('name',$cha['m_name']);
                //echo session('name');die();
                //print_r($cha);die();
                return view('ji',['cha'=>$cha]);
            }
            else
            {
                echo $this->error('用户名或邮箱输入错误！', 'index/admin/wang');
            }
        }
        else
        {
            return view('wang');
        }
    }
}